﻿using Models;
using strange.extensions.context.api;
using strange.extensions.dispatcher.eventdispatcher.api;
using strange.extensions.mediation.impl;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace CommonIoC
{
    public class PersonListPanelMediator : Mediator
    {

       

        [Inject]
        public PersonListPanelView view { get; set; }
        [Inject(ContextKeys.CONTEXT_DISPATCHER)]//全局的dispatcher
        public IEventDispatcher dispatcher { get; set; }

        public override void OnRegister()
        {
           
           // dispatcher.AddListener(PlayerGameState.IsDoingTalk, onIsDoingTalk);
            dispatcher.AddListener( TopButtonListEvent.ClickPersonList , onShowPersonList );

            dispatcher.AddListener(PersonInfoPanelEvent.ClickBack, onShowPersonList);

            dispatcher.AddListener(TopButtonListEvent.ClickBack, onTopButtonListClickBack);
            AddBackButtonClickFunc();
        }

        private void onTopButtonListClickBack(IEvent payload)
        {
            view.CloseThisUI();
        }

        private void onShowPersonList(IEvent payload)
        {
            UpdatePlayerData();
            view.ShowThisUI();
        }


        void AddBackButtonClickFunc()
        {
            if (view.btn_back != null)
            {
                view.btn_back.onClick.AddListener(delegate ()
                {

                    dispatcher.Dispatch( PersonListPanelEvent.ClickBack );
                    view.CloseThisUI();
                });
            }
        }

        void UpdatePlayerData()
        {
           // Dictionary<string,GamePerson> allplayer = GameDataManager.Instance.GetPersonDic();

           // int pcount = allplayer.Count;
            GamePerson person = GameDataManager.Instance.GetGamePerson();

            view.playerpanel[1].playerPanel.SetActive(false);
            view.playerpanel[2].playerPanel.SetActive(false);
            // Debug.Log("allplayer数组元素个数："+pcount);
            //if (pcount == 0)
            //{
            //    return;
            //}
            //if(pcount == 1)
            //{
            //    view.playerpanel[1].playerPanel.SetActive(false);
            //    view.playerpanel[2].playerPanel.SetActive(false);
            //}
            //if (pcount == 2)
            //{
            //    view.playerpanel[2].playerPanel.SetActive(false);
            //}
            //for (int i = 0; i < allplayer.Count; i++)
            //{
            // Debug.Log("inint初始化i的值为:"+ i );
            view.playerpanel[0].onClickSeePersonInfo.onClick.AddListener(delegate ()
                {
                    AddSeeInfoButtonClickFunc(0);
                    //这里自动+1 了 未知，，，
                });



                view.playerpanel[0].playerName.text = person.pbase.PlayerName;

                view.playerpanel[0].playerLv.text =string.Format("等级：{0}",
                                                     person.pbase.Lv.ToString() );

                view.playerpanel[0].playerHp.text =string.Format("生命:{0}/{1}",
                            person.pbase.NowHp.ToString() ,
                             person.pbase.MaxHp.ToString() );

              
            //}



        }


        void AddSeeInfoButtonClickFunc(int _index  )
        {
           // Debug.Log("AddSeeInfoButtonClickFunc:"+ _index );
            dispatcher.Dispatch(PersonListPanelEvent.ClickSeeOnePersonInfo ,  _index );
            view.CloseThisUI();

        }



        public override void OnRemove()
        {
            //dispatcher.RemoveListener(PlayerStageCommandEvent.canMove, onPlayerStageChangeTocanMove);
            //dispatcher.RemoveListener(PlayerStageCommandEvent.doTalk, onPlayerStageChangeTodoTalk);
            dispatcher.RemoveListener(TopButtonListEvent.ClickPersonList, onShowPersonList);

            dispatcher.RemoveListener(TopButtonListEvent.ClickBack, onTopButtonListClickBack);
            //view.dispatcher.RemoveListener(GameEvent.InputKey, onInputKey);
        }


    }
}
